using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShootThemUp.Sources.GameElements.Weapons;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ShootThemUp.Sources.GameElements.Ships.Ennemies.Fruits
{
    /// <summary>
    /// This class is an angry cherry.
    /// </summary>
    public class Cherry : Fruit
    {
        #region Cherry Texture Constant Fields

        /// <summary>
        /// The X position of the cherry texture in the spritesheet (in px).
        /// </summary>
        private const int SPRITESHEET_POSITION_X = 0;
        /// <summary>
        /// The Y position of the cherry texture in the spritesheet (in px).
        /// </summary>
        private const int SPRITESHEET_POSITION_Y = 140;
        /// <summary>
        /// The width of the cherry texture in the spritesheet (in px).
        /// </summary>
        private const int SPRITESHEET_WIDTH = 33;
        /// <summary>
        /// The height of the cherry texture in the spritesheet (in px).
        /// </summary>
        private const int SPRITESHEET_HEIGHT = 54;

        #endregion

        #region Cherry Shot Constant Fields

        /// <summary>
        /// The X position of the cherry shot texture in the spritesheet (in px).
        /// </summary>
        private const int SPRITESHEET_SHOT_POSTION_X = 16;
        /// <summary>
        /// The Y position of the cherry shot texture in the spritesheet (in px).
        /// </summary>
        private const int SPRITESHEET_SHOT_POSITION_Y = 83;
        /// <summary>
        /// The width of the cherry shot texture in the spritesheet (in px).
        /// </summary>
        private const int SPRITESHEET_SHOT_WIDTH = 20;
        /// <summary>
        /// The height of the cherry shot texture in the spritesheet (in px).
        /// </summary>
        private const int SPRITESHEET_SHOT_HEIGHT = 25;

        #endregion

        #region Cherry Animation Constant Fields

        /// <summary>
        /// The number of the frame to use to realise the animation linked to the fire.
        /// </summary>
        private const int FIRE_ANIMATION_FRAME_NUMBER = 2;
        /// <summary>
        /// The duration of the fire animation.
        /// </summary>
        private const float ANIMATION_DURATION_STEP = 0.003f;

        #endregion

        #region Cherry Model Constant Fields

        /// <summary>
        /// The score value of a cherry, ie the score earned by the player who destroy a cherry.
        /// </summary>
        private const int SCORE_VALUE = 200;
        /// <summary>
        /// The life of a cherry.
        /// </summary>
        private const int LIFE = 600;
        /// <summary>
        /// The time between two shot, in ms.
        /// </summary>
        private const int RELOAD_TIME = 3000;
        /// <summary>
        /// The weapon type of this cherry.
        /// </summary>
        private const WeaponType WEAPON = WeaponType.MultiSimpleShot;
        /// <summary>
        /// The number of shot of this cherry, fired after each reload time, at the same time.
        /// </summary>
        private const int NUMBER_OF_SHOT = 1;

        #endregion

        #region Fields

        /// <summary>
        /// The index of the current animation.
        /// </summary>
        private float index = 0;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="Cherry"/> class.
        /// </summary>
        public Cherry()
            : this(Cherry.RELOAD_TIME)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Cherry"/> class.
        /// </summary>
        /// <param name="delay">The delay before the first fire, in ms.</param>
        public Cherry(float delay)
            : base(Cherry.LIFE,
                    new Weapon(Cherry.WEAPON, Cherry.RELOAD_TIME, delay, Cherry.NUMBER_OF_SHOT,
                         new Rectangle(Cherry.SPRITESHEET_SHOT_POSTION_X, Cherry.SPRITESHEET_SHOT_POSITION_Y, Cherry.SPRITESHEET_SHOT_WIDTH, Cherry.SPRITESHEET_SHOT_HEIGHT)),
                    Cherry.SCORE_VALUE)
        {
            this.SourceRectangle = new Rectangle(Cherry.SPRITESHEET_POSITION_X + (int)index * Cherry.SPRITESHEET_WIDTH, Cherry.SPRITESHEET_POSITION_Y, Cherry.SPRITESHEET_WIDTH, Cherry.SPRITESHEET_HEIGHT);
            this.Speed = Vector2.Zero;
            this.Position = new Vector2(-Cherry.SPRITESHEET_WIDTH, 0);
        }

        #endregion

        #region Methods

        /// <summary>
        /// Call this function when the shot is done. Animate the cherry.
        /// </summary>
        public override void ShotDone()
        {
            this.index = Cherry.FIRE_ANIMATION_FRAME_NUMBER;
            base.ShotDone();
        }

        public override void Update(GameTime gameTime)
        {
            if (index > 0)
                this.index -= gameTime.ElapsedGameTime.Milliseconds * Cherry.ANIMATION_DURATION_STEP;

            this.SourceRectangle = new Rectangle(Cherry.SPRITESHEET_POSITION_X + (int)index * Cherry.SPRITESHEET_WIDTH, Cherry.SPRITESHEET_POSITION_Y, Cherry.SPRITESHEET_WIDTH, Cherry.SPRITESHEET_HEIGHT);

            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
        }


        #endregion
    }
}
